using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Documents.Common.Helpers
{
    public static class DateTimeFormatter
    {
        static string[] dateFormats;
        static string DefaultDateFormat = "dd MMM yyyy";

        static DateTimeFormatter()
            {
                dateFormats = new string[] {
                    "yyyyMMdd",
                    "yyyyMMdd HH:mm:ss",
                    "yyyyMMdd HH:mm:ss.f",
                    "yyyyMMdd HH:mm:ss.ff",
                    "yyyyMMdd HH:mm:ss.fff",
                    "yyyyMMdd HH:mm",
                    "yyyy-MM-dd",
                    "yyyy-MM-dd HH:mm:ss",
                    "yyyy-MM-dd HH:mm:ss.f",
                    "yyyy-MM-dd HH:mm:ss.ff",
                    "yyyy-MM-dd HH:mm:ss.fff",
                    "yyyy-MM-dd HH:mm",
                    "dd MMM yyyy",
                    "dd-MMM-yyyy",
                    "dd MM yyyy",
                    "dd-MM-yyyy",
                    "dd/MM/yyyy",
                    "dd MMM yyyy HH:mm:ss",
                    "dd MMM yyyy HH:mm",
                    "dd-MMM-yyyy HH:mm",
                    "dd-MMM-yyyy HH:mm:ss",
                    "dd-MMM-yyyy HH:mm:ss.f",
                    "dd-MMM-yyyy HH:mm:ss.ff",
                    "dd-MMM-yyyy HH:mm:ss.fff",
                    "dd MM yyyy HH:mm:ss",
                    "dd-MM-yyyy HH:mm",
                    "dd-MM-yyyy HH:mm:ss",
                    "dd-MM-yyyy HH:mm:f",
                    "dd-MM-yyyy HH:mm:ff",
                    "dd-MM-yyyy HH:mm:fff",
                    "dd/MM/yyyy HH:mm:ss",
                    "dd/MM/yyyy HH:mm",
                    "dd/MM/yyyy HH:mm:ss",
                    "dd/MM/yyyy HH:mm:f",
                    "dd/MM/yyyy HH:mm:ff",
                    "dd/MM/yyyy HH:mm:fff",
                    "dd/MM/yyyy HH:mm:ss",
                    "yyyy-MM-dd-HH-mm-ss",
                    "HH:mm",
                    "HH:mm:ss"
                };
            }

        
        public static string DefaultSmallDateTime
        {
            get { return "dd/MM/yyyy HH:mm"; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ParseDateTime(string date)
        {
            return ParseDateTime(date, DefaultDateFormat);
        }

        public static DateTime ParseDateTimeAsDateTime(string date)
        {
            DateTime ReturnDate = DateTime.MinValue;
            if (date != null && !string.IsNullOrEmpty(date.Trim()))
            {
                try
                {
                    //return ReturnDate;
                     return DateTime.ParseExact(date, dateFormats, null, DateTimeStyles.None);
                }
                catch(Exception ex)
                {
                    throw new Exception("Could not format '" + date + "' as valid DateTime object.", ex);
                }
            }
            return ReturnDate;
        }

        public static DateTime ParseDateTimeAsDateTime(string date,string formatter)
        {
            if (!string.IsNullOrEmpty(date.Trim()))
            {
                try
                {
                    return DateTime.ParseExact(date,formatter, null, DateTimeStyles.None);
                }
                catch (Exception ex)
                {
                    throw new Exception("Could not format '" + date + "' as valid DateTime object.", ex);
                }
            }
            else
                return DateTime.MinValue;
        }



        public static string ParseDateTime(string date, string formatter)
        {
            if (!string.IsNullOrEmpty(date.Trim()))
            {
                try
                {
                    return DateTime.ParseExact(date, dateFormats, null, DateTimeStyles.None).ToString(formatter);
                }
                catch (Exception ex)
                {
                    throw new Exception("Could not format '"+date+"' as valid DateTime object.",ex);
                }
            }
            else
                return "";
        }

        public static string FormatDate(object colDate, string formatter)
        {
            return FormatDate(colDate, formatter, false);
        }

        public static string FormatDate(object colDate, string formatter, bool emptyIfInvalidDate)
        {
            if (colDate != null && colDate.ToString().Trim().Length > 0)
            {
                try
                {
                    return DateTimeFormatter.ParseDateTime(colDate.ToString(), formatter);
                }
                catch
                {
                    if (!emptyIfInvalidDate) throw;
                }
            }
            return string.Empty;
        }

        public static int ToUnixTime(DateTime dt)
        {
            DateTime start = new DateTime(1970, 1, 1).ToLocalTime();
            TimeSpan t = (TimeSpan)(dt.Subtract(start));
            return (int) t.TotalSeconds;
        }
    }
}
